package com.example.demo.web.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.example.demo.common.dto.MessageDTO;
import com.example.demo.common.dto.UserDTO;
import com.example.demo.config.service.RocketMQProducerService;
import com.example.demo.web.service.UserService;
import java.nio.charset.StandardCharsets;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author yongen
 * @description: 用户业务实现
 * @date 2022/2/9 2:40 PM
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

  @Resource
  private RocketMQProducerService rocketMQProducerService;

  @Override
  public void saveUser(UserDTO userDTO) {
    // 1.模拟业务操作
    // ....
    // 2.向关联系统发送消息
    MessageDTO messageDTO = new MessageDTO();
    messageDTO.setTopic("T_DEMO_PROJECT");
    messageDTO.setTag("ADD_USER");
    messageDTO.setKey(IdUtil.simpleUUID());
    messageDTO.setContent(JSON.toJSONString(userDTO).getBytes(StandardCharsets.UTF_8));
    boolean success = rocketMQProducerService.sendMessage(messageDTO);
    if (success) {
      log.info("消息发送成功, UserServiceImpl.saveUser");
    } else {
      log.info("消息发送失败");
      // TODO 记录错误记录
    }
  }
}
